package org.ssssssss.example.mail;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import java.io.IOException;

public class readExcelFile {
    public static void main(String[] args) throws Exception {
        String excelPath = "F:\\mail\\test.xlsx";
        try {
            //String encoding = "GBK";
            File excel = new File(excelPath);
            if (excel.isFile() && excel.exists()) {   //判断文件是否存在

                String[] split = excel.getName().split("\\.");  //.是特殊字符，需要转义！！！！！

                Workbook wb;
                FileInputStream fis = new FileInputStream(excel);
                //根据文件后缀（xls/xlsx）进行判断
                if ("xls".equals(split[1])) {
                       //文件流对象
                    wb = new HSSFWorkbook(fis);
                }
                  else if ("xlsx".equals(split[1])) {
                   wb = new XSSFWorkbook(fis);
                   //wb = new SXSSFWorkbook((XSSFWorkbook) wb);
               }
                else {
                    System.out.println("文件类型错误!");
                    return;
                }

                //开始解析
                Sheet sheet = wb.getSheetAt(0);     //读取sheet 0

                int firstRowIndex = sheet.getFirstRowNum() + 1;   //第一行是列名，所以不读
                int lastRowIndex = sheet.getLastRowNum();
                System.out.println("firstRowIndex: " + firstRowIndex);
                System.out.println("lastRowIndex: " + lastRowIndex);

//                ArrayList<List<String>> list=new ArrayList<List<String>>();
//                ArrayList<String> string=new ArrayList <String>();//一维数组
//                ArrayList<String> string1=new ArrayList <String>();//一维数组

                for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                    System.out.println("rIndex: " + rIndex);//关键字
                    Row row = sheet.getRow(rIndex);
                    if (row != null) {
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                            Cell cell = row.getCell(cIndex);
                            if (cell != null) {
                                System.out.println(cell.toString());
                            }
                        }
                    }
                }

            } else {
                System.out.println("找不到指定的文件");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
